package net.sevencolorlotus.ch01;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.CharsetUtil;

/**
 * @Description: 客户端消息处理
 * @Author: Jason.zhu
 * @Create: 2020/03/04 13:52
 */

public class EchoClientHandler extends SimpleChannelInboundHandler<ByteBuf> {
    /**
     * 客户端获取到数据干什么
     * @param channelHandlerContext ChannelHandlerContext
     * @param msg ByteBuf
     * @throws Exception
     */
    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf msg) throws Exception {
        System.out.println("Client accept:" + msg.toString(CharsetUtil.UTF_8));
    }

    /**
     * 客户端被通知channel活跃后，做事
     * @param ctx
     */
    public void channelActive(ChannelHandlerContext ctx){
        // 往服务器写数据
        ctx.writeAndFlush(Unpooled.copiedBuffer("Hello,Netty", CharsetUtil.UTF_8));
    }
}
